/*
 * @author java_deng
 * @date 2025/3/11 21:12
 * @description
 */
package org.kaka.service.impl;

import org.kaka.dto.EmpDto;
import org.kaka.dto.InstitutionDto;
import org.kaka.mapper.EmpMapper;
import org.kaka.mapper.InstitutionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private InstitutionMapper institutionMapper;

    public List<EmpDto> list(EmpDto empDto) {
        List<EmpDto> list = empMapper.list(empDto);
        list.forEach( item -> {
            Long institutionId = item.getInstitutionId();
            if(institutionId!=null){
                InstitutionDto institutionDto = institutionMapper.selectById(institutionId);
                Integer level = institutionDto.getLevel();
                if (level > 1) {
                    Integer parentId = institutionDto.getParentId();
                    if (parentId != null) {
                        institutionDto.setParentName(institutionMapper.selectById(institutionDto.getParentId()).getName());
                    }
                }
                StringBuffer fullName = new StringBuffer(institutionDto.getName());
                while (level > 1) {
                    Integer itemParentId = institutionDto.getParentId();
                    if (itemParentId != null) {
                        InstitutionDto parent = institutionMapper.selectById(itemParentId);
                        fullName.insert(0, parent.getName() + "/");
                        level = parent.getLevel();
                        institutionDto=parent;
                    } else {
                        break;
                    }

                }
                item.setInstitutionName(fullName.toString());
            }
        });
        return list;
    }

    public void save(EmpDto empDto) {
         empMapper.insert(empDto);
    }

    public void update(EmpDto empDto) {
        empMapper.updateById(empDto);
    }

    public void delete(Long id) {
        empMapper.deleteById(id);
    }

    public EmpDto detail(Long id) {
        EmpDto empDto = empMapper.selectById(id);
        Long institutionId = empDto.getInstitutionId();
        if(institutionId!=null) {
            InstitutionDto institutionDto = institutionMapper.selectById(institutionId);
            Integer level = institutionDto.getLevel();
            if (level > 1) {
                Integer parentId = institutionDto.getParentId();
                if (parentId != null) {
                    institutionDto.setParentName(institutionMapper.selectById(institutionDto.getParentId()).getName());
                }
            }
            StringBuffer fullName = new StringBuffer(institutionDto.getName());
            while (level > 1) {
                Integer itemParentId = institutionDto.getParentId();
                if (itemParentId != null) {
                    InstitutionDto parent = institutionMapper.selectById(itemParentId);
                    fullName.insert(0, parent.getName() + "/");
                    level = parent.getLevel();
                    institutionDto=parent;
                } else {
                    break;
                }
            }
            empDto.setInstitutionName(fullName.toString());
        }
        return empDto;
    }
}
